package org.example.utlis;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.example.constant.BaseConstant;
import org.example.dto.SearchItemDto;

import java.util.List;

/**
 * MybatisPlus搜索工具类
 * @author joey
 */
public class MybatisPlusSearchUtil {
    /**
     * eq=等于，ne=不等于，like=全模糊，likeLeft=左模糊，likeRight=右模糊，
     * notLike=不模糊，gt=大于等于，lt=小于等于，ge=大于，le=小于
     */
    public static final String EQ="eq";
    public static final String NE="ne";
    public static final String LIKE="like";
    public static final String LIKE_LEFT="likeLeft";
    public static final String LIKE_RIGHT="likeRight";
    public static final String NOT_LIKE="notLike";
    public static final String GT="gt";
    public static final String LT="lt";
    public static final String GE="ge";
    public static final String LE="le";
    public static final String IN="in";

    /**
     * 构造QueryWrapper
     * @param searchJson
     * @return
     */
    public static QueryWrapper parseWhereSql(String searchJson){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StrUtil.isNotEmpty(searchJson)){
            try {
                List<SearchItemDto> searchList = JSON.parseArray(searchJson, SearchItemDto.class);
                if(CollUtil.isNotEmpty(searchList)){
                    for(SearchItemDto form : searchList){
                        if(StringUtils.isBlank(form.getKey())){
                            continue;
                        }
                        if(StringUtils.isBlank(form.getValue())){
                            continue;
                        }
                        if(StrUtil.isEmpty(form.getType())){
                            queryWrapper.eq(form.getKey(),form.getValue());
                        }else {
                            switch (form.getType()){
                                case EQ : queryWrapper.eq(form.getKey(),form.getValue());break;
                                case NE : queryWrapper.ne(form.getKey(),form.getValue());break;
                                case LIKE : queryWrapper.like(form.getKey(),form.getValue());break;
                                case LIKE_LEFT : queryWrapper.likeLeft(form.getKey(),form.getValue());break;
                                case LIKE_RIGHT : queryWrapper.likeRight(form.getKey(),form.getValue());break;
                                case NOT_LIKE : queryWrapper.notLike(form.getKey(),form.getValue());break;
                                case GT : queryWrapper.gt(form.getKey(),form.getValue());break;
                                case LT : queryWrapper.lt(form.getKey(),form.getValue());break;
                                case GE : queryWrapper.ge(form.getKey(),form.getValue());break;
                                case LE : queryWrapper.le(form.getKey(),form.getValue());break;
                                case IN : queryWrapper.in(form.getKey(),form.getValue().split(BaseConstant.SPITS_STR1));break;
                                default:
                            }
                        }
                    }
                }
            }catch (Throwable e){
                //吃掉异常
            }
        }
        return queryWrapper;
    }
}
